﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace GoShopping.Web.ProductManager
{
    public partial class IndexConfig : System.Web.UI.Page
    {
        #region Initial Object
        BLL.PrdctInfo.DiscountPrdctBean dpb = new GoShopping.BLL.PrdctInfo.DiscountPrdctBean();
        BLL.PrdctInfo.RecomendPrdctBean rpb = new GoShopping.BLL.PrdctInfo.RecomendPrdctBean();
        Model.DiscountPrdct dp = new GoShopping.Model.DiscountPrdct();
        Model.RecomendPrdct rp = new GoShopping.Model.RecomendPrdct();
        BLL.ProductBean pb = new GoShopping.BLL.ProductBean();
        Model.Product prdt = new GoShopping.Model.Product();
        #endregion

        private const string filePath = @"~/Config/PrdtConfig.xml";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //SetDLLType();
                GetPrdtDataByType(extGVPromotedPrdt, null);
                GetConfigedList("Pro");
            }
        }
        #region Common Method
        private void GetTreeView()
        {
            TreeView tv = Master.MasterTreeView;
            SetControlCommon.GetPrdtTreeView(tv, Server.MapPath("Navigation.xml"), "prdt");
        }

        void GetPrdtDataByType(GridView gv, string filterString)
        {
            switch (gv.ID)
            {
                case "extGVPromotedPrdt":
                    {
                        gv.DataSource = dpb.GetAll();
                        gv.DataBind();
                    } break;
                case "extGVRecPrdt":
                    {
                        gv.DataSource = rpb.GetAll();
                        gv.DataBind();
                    } break;
                case "extGVMainPrdt":
                case "gvSpecial":
                    {
                        if (string.IsNullOrEmpty(filterString))
                        {
                            gv.DataSource = pb.GetAllProduct();
                            gv.DataBind();
                        }
                        else
                        {
                            gv.DataSource = pb.GetProductList(filterString);
                            gv.DataBind();
                        }
                    } break;
                default:
                    break;
            }
        }

        void SetDLLType()
        {
            //SetControlCommon.GetDDLType(ddlType);
            //SetControlCommon.GetDDLType(ddlType1);
            //SetControlCommon.GetDDLType(ddlType2);
            //SetControlCommon.GetDDLType(ddlSpecialType);
        }

        public string GetSupplierName(object obj)
        {
            BLL.ProductSuplyBean psb = new GoShopping.BLL.ProductSuplyBean();
            return psb.GetProductSuply(Convert.ToInt32(obj)).Name;
        }
        /// <summary>
        /// 获取配置的节点
        /// </summary>
        /// <param name="flag">值为:"Pro,Rec,Main,Brand,Friend"</param>
        public void GetConfigedList(string flag)
        {
            string xPath = "";
            switch (flag)
            {
                case "Pro":
                    {
                        xPath = "//Index/Promoted";
                        extGVProPrdt.DataSource = XMLManipulation.GetConfig(Server.MapPath(filePath), xPath);
                        extGVProPrdt.DataBind();
                    } break;
                case "Rec":
                    {
                        xPath = "//Index/Recommand";
                        extGVRecommandedPrdt.DataSource = XMLManipulation.GetConfig(Server.MapPath(filePath), xPath);
                        extGVRecommandedPrdt.DataBind();
                    } break;
                case "Main":
                    {
                        xPath = "//Index/MainPrdt";
                        extGVMainPrdtList.DataSource = XMLManipulation.GetConfig(Server.MapPath(filePath), xPath);
                        extGVMainPrdtList.DataBind();
                    } break;
                case "Brand":
                    {
                        xPath = "//Index/Brand";
                        extBrandList.DataSource = XMLManipulation.GetConfig(Server.MapPath(filePath), xPath);
                        extBrandList.DataBind();
                    } break;
                case "Friend":
                    {
                        xPath = "//Index/FriendlyLink";
                        extFriendList.DataSource = XMLManipulation.GetConfig(Server.MapPath(filePath), xPath);
                        extFriendList.DataBind();
                    } break;
                case "Special":
                    {
                        xPath = "//Index/Special";
                        gvSpeicalList.DataSource = XMLManipulation.GetConfig(Server.MapPath(filePath), xPath);
                        gvSpeicalList.DataBind();
                    } break;
                default:
                    break;
            }
        }
        #endregion

        #region 导航设置
        protected void btnDiscountSet_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 0;
            GetPrdtDataByType(extGVPromotedPrdt, null);
            GetConfigedList("Pro");
            //GetDiscountOrRecommandedGV(extGVProPrdt);
        }

        protected void btnRecSet_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 1;
            GetPrdtDataByType(extGVRecPrdt, null);
            GetConfigedList("Rec");
            //GetDiscountOrRecommandedGV(extGVRecommandedPrdt);
        }

        protected void btnMainPrdt_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 2;
            GetPrdtDataByType(extGVMainPrdt, null);
            GetConfigedList("Main");
            //GetDiscountOrRecommandedGV(extGVMainPrdt);
        }

        protected void btnBrand_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 3;
            GoShopping.BLL.DicBrandBean dbb = new GoShopping.BLL.DicBrandBean();
            extBrand.DataSource = dbb.GetAllBrand();
            extBrand.DataBind();
            GetConfigedList("Brand");
        }

        protected void btnFriendlyLink_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 4;
            GoShopping.BLL.FriendlyLinkBean flb = new GoShopping.BLL.FriendlyLinkBean();
            extFriend.DataSource = flb.GetAllFriendlyLink();
            extFriend.DataBind();
            GetConfigedList("Friend");
        }

        protected void btnSpeical_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 5;
            GetPrdtDataByType(gvSpecial, null);
            GetConfigedList("Special");
        }
        #endregion

        #region 打折商品设置
        protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (ddlType.SelectedValue == "-1")
            //{
            //    GetPrdtDataByType(extGVPromotedPrdt, null);
            //    return;
            //}
            //string filterStr = "AND ProductTypeID=" + ddlType.SelectedValue;
            //GetPrdtDataByType(extGVPromotedPrdt, filterStr);
        }

        protected void extGVPromotedPrdt_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //if (ddlType.SelectedValue == "-1")
            //{
            //    this.extGVPromotedPrdt.PageIndex = e.NewPageIndex;
            //    GetPrdtDataByType(extGVPromotedPrdt, null);
            //    return;
            //}
            //string filterStr = "AND ProductTypeID=" + ddlType.SelectedValue;
            this.extGVPromotedPrdt.PageIndex = e.NewPageIndex;
            GetPrdtDataByType(extGVPromotedPrdt, null);
        }
        /// <summary>
        /// 商品列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void extGVPromotedPrdt_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "PrdtEdit")
            {
                hidPrdtId.Value = e.CommandArgument.ToString();
                lblName.Text = pb.GetProduct(Convert.ToInt32(e.CommandArgument)).Name;
                hidImgPro.Value = BLL.ProductImgBean.GetProductImgPath(Convert.ToInt32(e.CommandArgument), new BLL.ProductBean().GetProduct(Convert.ToInt32(e.CommandArgument)).ProductTypeID);
            }
            else if (e.CommandName == "PrdtDel")
            {

            }
        }

        protected void btnAddPromoted_Click(object sender, EventArgs e)
        {
            //dp.PrdctID = Convert.ToInt32(hidPrdtId.Value);
            //dp.DiscountPrice = Convert.ToDecimal(zlTxtOffPrice.Text.Trim());
            //dp.StartDate = Convert.ToDateTime(zlTxtStartTime.Text.Trim());
            //dp.EndDate = Convert.ToDateTime(zlTxtEndTime.Text.Trim());
            //dpb.AddOne(dp);
        }

        protected void extGVProPrdt_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }

        /// <summary>
        /// 打折商品列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void extGVProPrdt_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "PrdtDel")
            {
                XMLManipulation.DelNode(Server.MapPath(filePath), "//Index/Promoted/pic[@Id='" + e.CommandArgument.ToString() + "']");
                GetConfigedList("Pro");
            }
        }
        #endregion

        #region 推荐商品设置
        protected void ddlType1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (ddlType1.SelectedValue == "-1")
            //{
            //    GetPrdtDataByType(extGVRecPrdt, null);
            //    return;
            //}
            //string filterStr = "AND ProductTypeID=" + ddlType1.SelectedValue;
            //GetPrdtDataByType(extGVRecPrdt, filterStr);
        }

        protected void extGVRecPrdt_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //if (ddlType1.SelectedValue == "-1")
            //{
            //    this.extGVRecPrdt.PageIndex = e.NewPageIndex;
            //    GetPrdtDataByType(extGVRecPrdt, null);
            //    return;
            //}
            //string filterStr = "AND ProductTypeID=" + ddlType1.SelectedValue;
            this.extGVRecPrdt.PageIndex = e.NewPageIndex;
            GetPrdtDataByType(extGVRecPrdt, null);
        }

        protected void extGVRecPrdt_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "PrdtEdit")
            {
                hidPrdtId1.Value = e.CommandArgument.ToString();
                lblRecPrdt.Text = pb.GetProduct(Convert.ToInt32(e.CommandArgument)).Name;
                hidImgRec.Value = BLL.ProductImgBean.GetProductImgPath(Convert.ToInt32(e.CommandArgument), new BLL.ProductBean().GetProduct(Convert.ToInt32(e.CommandArgument)).ProductTypeID);
            }
            else if (e.CommandName == "btnDel")
            {

            }
        }

        protected void btnAddRecPrdt_Click(object sender, EventArgs e)
        {
            rp.Prdctid = Convert.ToInt32(hidPrdtId1.Value);
            rp.Reason = txtReason.Text.Trim();

            rpb.AddOne(rp);
        }

        /// <summary>
        /// 配置后的列表事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void extGVRecommandedPrdt_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }

        /// <summary>
        /// 配置后的列表事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void extGVRecommandedPrdt_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "PrdtDel")
            {
                XMLManipulation.DelNode(Server.MapPath(filePath), "//Index/Recommand/pic[@Id='" + e.CommandArgument.ToString() + "']");
                GetConfigedList("Rec");
            }
        }
        #endregion

        #region 主商品设置
        protected void ddlType2_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (ddlType2.SelectedValue == "-1")
            //{
            //    GetPrdtDataByType(extGVMainPrdt, null);
            //    return;
            //}
            //string filterStr = "AND ProductTypeID=" + ddlType2.SelectedValue;
            //GetPrdtDataByType(extGVMainPrdt, filterStr);
        }

        protected void extGVMainPrdt_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //if (ddlType2.SelectedValue == "-1")
            //{
            //    this.extGVMainPrdt.PageIndex = e.NewPageIndex;
            //    GetPrdtDataByType(extGVMainPrdt, null);
            //    return;
            //}
            //string filterStr = "AND ProductTypeID=" + ddlType2.SelectedValue;
            this.extGVMainPrdt.PageIndex = e.NewPageIndex;
            GetPrdtDataByType(extGVMainPrdt, null);
        }

        protected void extGVMainPrdt_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "PrdtEdit")
            {
                hidMain.Value = e.CommandArgument.ToString();
                lblMainPrdt.Text = pb.GetProduct(Convert.ToInt32(e.CommandArgument)).Name;
                hidImgMain.Value = BLL.ProductImgBean.GetProductImgPath(Convert.ToInt32(e.CommandArgument), new BLL.ProductBean().GetProduct(Convert.ToInt32(e.CommandArgument)).ProductTypeID);
            }
        }

        protected void extGVMainPrdtList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "PrdtDel")
            {
                XMLManipulation.DelNode(Server.MapPath(filePath), "//Index/MainPrdt/pic[@Id='" + e.CommandArgument.ToString() + "']");
                GetConfigedList("Main");
            }
        }
        #endregion

        #region 专卖店设置
        protected void extBrandList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "BrandDel")
            {
                XMLManipulation.DelNode(Server.MapPath(filePath), "//Index/Brand/pic[@Id='" + e.CommandArgument.ToString() + "']");
                GetConfigedList("Brand");
            }
        }
        protected void extBrand_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            GoShopping.BLL.DicBrandBean dbb = new GoShopping.BLL.DicBrandBean();
            if (e.CommandName == "BrandEdit")
            {
                hidBrand.Value = e.CommandArgument.ToString();
                lblBrand.Text = dbb.GetBrand(Convert.ToInt32(e.CommandArgument)).Name;
                //需要一个根据ID查找Brand Name的方法
                //lblBrand.Text = pb.GetProduct(Convert.ToInt32(e.CommandArgument)).Name;
                //hidImgBrand.Value = BLL.ProductImgBean.GetProductImgPath(Convert.ToInt32(e.CommandArgument), new BLL.ProductTypeBean().GetProductType(Convert.ToInt32(e.CommandArgument)).ID);
            }
        }
        protected void extBrand_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GoShopping.BLL.DicBrandBean dbb = new GoShopping.BLL.DicBrandBean();
            this.extBrand.PageIndex = e.NewPageIndex;
            extBrand.DataSource = dbb.GetAllBrand();
            extBrand.DataBind();
        }
        #endregion

        #region 友情链接设置
        protected void extFriendList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "FriendDel")
            {
                XMLManipulation.DelNode(Server.MapPath(filePath), "//Index/FriendlyLink/pic[@Id='" + e.CommandArgument.ToString() + "']");
                GetConfigedList("Friend");
            }
        }
        protected void extFriend_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            BLL.FriendlyLinkBean flb = new GoShopping.BLL.FriendlyLinkBean();
            Model.FriendlyLink fl = new GoShopping.Model.FriendlyLink();
            fl = flb.GetFriendlyLink(Convert.ToInt32(e.CommandArgument));
            if (e.CommandName == "FriendEdit")
            {
                hidFriend.Value = e.CommandArgument.ToString();
                lblFriendlyLink.Text = fl.Title;
                hidImgFriend.Value = fl.LinkUrl;
                //需要一个根据友情链接ID得到Name的接口
                //lblFriendlyLink.Text = pb.GetProduct(Convert.ToInt32(e.CommandArgument)).Name;
                //hidImgFriend.Value = BLL.ProductImgBean.GetProductImgPath(Convert.ToInt32(e.CommandArgument), new BLL.ProductTypeBean().GetProductType(Convert.ToInt32(e.CommandArgument)).ID);
            }
        }
        protected void extFriend_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.extFriend.PageIndex = e.NewPageIndex;
            GoShopping.BLL.FriendlyLinkBean flb = new GoShopping.BLL.FriendlyLinkBean();
            extFriend.DataSource = flb.GetAllFriendlyLink();
            extFriend.DataBind();
        }
        #endregion

        #region 特殊商品设置
        protected void ddlSpecialType_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (ddlSpecialType.SelectedValue == "-1")
            //{
            //    GetPrdtDataByType(gvSpecial, null);
            //    return;
            //}
            //string filterStr = "AND ProductTypeID=" + ddlSpecialType.SelectedValue;
            //GetPrdtDataByType(gvSpecial, filterStr);
        }
        protected void gvSpecial_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //if (ddlSpecialType.SelectedValue == "-1")
            //{
            //    this.gvSpecial.PageIndex = e.NewPageIndex;
            //    GetPrdtDataByType(gvSpecial, null);
            //    return;
            //}
            //string filterStr = "AND ProductTypeID=" + ddlSpecialType.SelectedValue;
            this.gvSpecial.PageIndex = e.NewPageIndex;
            GetPrdtDataByType(gvSpecial, null);
        }
        protected void gvSpecial_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "PrdtEdit")
            {
                hidSpecial.Value = e.CommandArgument.ToString();
                lblSpecialName.Text = pb.GetProduct(Convert.ToInt32(e.CommandArgument)).Name;
                hidImgSpecial.Value = BLL.ProductImgBean.GetProductImgPath(Convert.ToInt32(e.CommandArgument), new BLL.ProductBean().GetProduct(Convert.ToInt32(e.CommandArgument)).ProductTypeID);
            }
        }
        protected void gvSpeicalList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }
        protected void gvSpeicalList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "PrdtDel")
            {
                XMLManipulation.DelNode(Server.MapPath(filePath), "//Index/Special/pic[@Id='" + e.CommandArgument.ToString() + "']");
                GetConfigedList("Special");
            }
        }
        protected void btnSpecialSearch_Click(object sender, EventArgs e)
        {

        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {

        }
        #endregion

        #region Button click event which config data for index
        string file = "";
        /// <summary>
        /// 折扣产品配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnIndexPro_Click(object sender, EventArgs e)
        {
            string xPath = "//Index/Promoted";
            file = Server.MapPath(filePath);
            ConfigNode cn = new ConfigNode(Convert.ToInt32(hidPrdtId.Value), lblName.Text, GetWebPath(hidImgPro.Value), txtProDescrip.Text);
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), null, "<script>alert('" + XMLManipulation.WriteToConfig(file, xPath, cn).ToString() + "')</script>", false);
            GetConfigedList("Pro");
        }
        /// <summary>
        /// 推荐产品配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnIndexRec_Click(object sender, EventArgs e)
        {
            string xPath = "//Index/Recommand";
            file = Server.MapPath(filePath);
            ConfigNode cn = new ConfigNode(Convert.ToInt32(hidPrdtId1.Value), lblRecPrdt.Text, GetWebPath(hidImgRec.Value), txtReason.Text);
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), null, "<script>alert('" + XMLManipulation.WriteToConfig(file, xPath, cn).ToString() + "')</script>", false);
            GetConfigedList("Rec");
        }
        /// <summary>
        /// 主商品配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnIndexMain_Click(object sender, EventArgs e)
        {
            string xPath = "//Index/MainPrdt";
            file = Server.MapPath(filePath);
            ConfigNode cn = new ConfigNode(Convert.ToInt32(hidMain.Value), lblMainPrdt.Text, GetWebPath(hidImgMain.Value, "main"), txtMainDescript.Text);
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), null, "<script>alert('" + XMLManipulation.WriteToConfig(file, xPath, cn).ToString() + "')</script>", false);
            GetConfigedList("Main");
        }
        /// <summary>
        /// 专卖点配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnIndexBrand_Click(object sender, EventArgs e)
        {
            string xPath = "//Index/Brand";
            file = Server.MapPath(filePath);
            ConfigNode cn = new ConfigNode(Convert.ToInt32(hidBrand.Value), lblBrand.Text, "", txtBrandDescrip.Text);
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), null, "<script>alert('" + XMLManipulation.WriteToConfig(file, xPath, cn).ToString() + "')</script>", false);
            GetConfigedList("Brand");
        }
        /// <summary>
        /// 友情链接配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnIndexFriend_Click(object sender, EventArgs e)
        {
            string xPath = "//Index/FriendlyLink";
            file = Server.MapPath(filePath);
            ConfigNode cn = new ConfigNode(Convert.ToInt32(hidFriend.Value), lblFriendlyLink.Text, hidImgFriend.Value, txtFriendDescrip.Text);
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), null, "<script>alert('" + XMLManipulation.WriteToConfig(file, xPath, cn).ToString() + "')</script>", false);
            GetConfigedList("Friend");
        }
        /// <summary>
        /// 特殊商品设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAddToIndex_Click(object sender, EventArgs e)
        {
            string xPath = "//Index/Special";
            file = Server.MapPath(filePath);
            ConfigNode cn = new ConfigNode(Convert.ToInt32(hidSpecial.Value), lblSpecialName.Text, GetWebPath(hidImgSpecial.Value), txtSpecialDescription.Text);
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), null, "<script>alert('" + XMLManipulation.WriteToConfig(file, xPath, cn).ToString() + "')</script>", false);
            GetConfigedList("Special");
        }
        string GetWebPath(string path)
        {
            return GoShopping.Utility.WebPath.ConvertSpecifiedPathToRelativePath(this, path);
        }
        /// <summary>
        /// For control which are not asp.net server control
        /// </summary>
        /// <param name="path"></param>
        /// <param name="flag"></param>
        /// <returns></returns>
        string GetWebPath(string path, string flag)
        {
            return GoShopping.Utility.WebPath.ConvertSpecifiedPathToRelativePath(this, path).Replace("~", ".");
        }
        #endregion
    }
}